package com.membership.stumembership.contrller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.membership.stumembership.domain.Membership;
import com.membership.stumembership.domain.Student;
import com.membership.stumembership.service.StudentService;
import com.membership.stumembership.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
@RequiredArgsConstructor
public class StudentController {
    private final StudentService studentService;

    @RequestMapping("/getAll")
    public Object selectAll() {
        return studentService.list((Wrapper<Student>) null);
    }

    @RequestMapping("/selectByPage")
    public Object selectByPage(Student stu, Page<Student> page){
        return studentService.selectByPage(page,stu);
    }
    @RequestMapping("/add")
    public Object add(@RequestBody Student student) {
        Student stu =
                studentService.getOne(new QueryWrapper<Student>().eq("stu_name", student.getStuName()));
        if (stu != null) {
            return new Result(0, "添加失败，该姓名已存在！");
        }
        return studentService.save(student);
    }
    @RequestMapping("/delete")
    public Object delete(@RequestBody Student student){
        return studentService.removeById(student.getStuId());
    }

    @RequestMapping("/update")
    public Object update(@RequestBody Student student){
        return studentService.updateById(student);
    }
}

